<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types=1);

namespace app\api\controller;

use app\api\model\Order as OrderModel;
use app\common\service\wxpush\WXBizMsgCrypt;
use cores\exception\BaseException;
use think\response\Json;

/**
 * 微信消息推送
 * Class WxPush
 * @package app\api\controller
 */
class WxPush extends Controller
{
    private function checkSignature($signature,$timestamp,$nonce)
    {
        $token = 'bq5xbsqLV3D948Sbgq2ii0ll'; //和设置的Token一致
        $tmpArr = array($token, $timestamp, $nonce);
        sort($tmpArr, SORT_STRING);
        $tmpStr = implode($tmpArr);
        $tmpStr = sha1($tmpStr);
        //前面的全不用动,主要是给服务器返回的部分,我网上搜的又是转字符串,又是干啥的,很多都没用
        if ($tmpStr == $signature) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 微信消息推送地址https://shopdev.bangnuo.top/index.php?s=/api/wxPush/wxReceipt
     * @return XML
     * @throws BaseException
     */
    public function wxReceipt()
    {
        $log_file = runtime_root_path() .'api/log/wx_Receipt_'.date('y-m-d').'.log';
        file_put_contents($log_file, print_r($_REQUEST, true)."\n", FILE_APPEND);

        $xmlData = file_get_contents('php://input');
        file_put_contents($log_file, print_r($xmlData, true)."\n", FILE_APPEND);
        $xml = simplexml_load_string($xmlData);
        file_put_contents($log_file, print_r($xml, true)."\n", FILE_APPEND);

        if (isset($_GET["echostr"])) {
            $signature = $_GET['signature'];
            $timestamp = $_GET['timestamp'];
            $echostr = $_GET['echostr']; //接收传递字符串
            $nonce = $_GET['nonce'];
            $check = $this->checkSignature($signature, $timestamp, $nonce);
            if ($check) {
                ob_clean(); //这个位置
                echo $echostr;
            }
        }
        /*$pc = new WXBizMsgCrypt($token, $encodingAesKey, $appId);
        $encryptMsg = '';
        $errCode = $pc->encryptMsg($text, $timeStamp, $nonce, $encryptMsg);
        if ($errCode == 0) {
            print("加密后: " . $encryptMsg . "\n");
        } else {
            print($errCode . "\n");
        }

        $xml_tree = new DOMDocument();
        $xml_tree->loadXML($encryptMsg);
        $array_e = $xml_tree->getElementsByTagName('Encrypt');
        $array_s = $xml_tree->getElementsByTagName('MsgSignature');
        $encrypt = $array_e->item(0)->nodeValue;
        $msg_sign = $array_s->item(0)->nodeValue;

        $format = "<xml><ToUserName><![CDATA[toUser]]></ToUserName><Encrypt><![CDATA[%s]]></Encrypt></xml>";
        $from_xml = sprintf($format, $encrypt);

        // 第三方收到公众号平台发送的消息
        $msg = '';
        $errCode = $pc->decryptMsg($msg_sign, $timeStamp, $nonce, $from_xml, $msg);
        if ($errCode == 0) {
            print("解密后: " . $msg . "\n");
        } else {
            print($errCode . "\n");
        }*/
        /*$orderId = '';
        $model = OrderModel::getDetail($orderId);
        if ($model->receipt()) {
            return $this->renderSuccess('确认收货成功');
        }
        return $this->renderError($model->getError());*/
    }

}
